package com.viphuli.adapter;

import java.util.List;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;

import com.loopj.android.http.RequestParams;
import com.offroader.utils.OnlineParamUtils;
import com.offroader.utils.PhoneUtils;
import com.offroader.utils.ViewUtils;
import com.viphuli.app.R;
import com.viphuli.common.AccessTokenKeeper;
import com.viphuli.common.Constants;
import com.viphuli.common.MyApp;
import com.viphuli.common.MyPageHelper;
import com.viphuli.fragment.AccountLoginFragment;
import com.viphuli.fragment.PurchaseOrderFragment;
import com.viphuli.http.ApiRequest;
import com.viphuli.http.bean.part.PurchaseOrder;

public class OrderAdapter extends BaseAdapter implements OnClickListener {

	private PurchaseOrderFragment frag;
	private List<PurchaseOrder> list;

	public OrderAdapter(PurchaseOrderFragment frag, List<PurchaseOrder> list) {
		this.frag = frag;
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int index) {
		return list.get(index);
	}

	@Override
	public long getItemId(int index) {
		return index;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;

		if (convertView == null || convertView.getTag() == null) {
			convertView = ViewUtils.inflateView(parent.getContext(), R.layout.listview_order_item);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.cancelBtn.setVisibility(View.GONE);
		holder.payBtn.setVisibility(View.GONE);
		holder.contactCancelBtn.setVisibility(View.GONE);
		holder.commentBtn.setVisibility(View.GONE);

		//填充数据
		final PurchaseOrder item = list.get(position);
		holder.name.setText(item.getName());
		holder.status.setText(item.status());
		holder.time.setText(item.createTimeDay());
		holder.site.setText(item.address());

		//订单不同状态处理
		switch (item.getStatus()) {
		case Constants.ORDER_STATUS_WAIT_PAY://待付费状态

			holder.cancelBtn.setVisibility(View.VISIBLE);
			holder.payBtn.setVisibility(View.VISIBLE);
			holder.price.setText(item.price());//价格
			holder.priceName.setText(MyApp.getInstance().getString(R.string.text_order_title_goods));//价格

			break;
		case Constants.ORDER_STATUS_WAIT_ACCEPT:

			holder.contactCancelBtn.setVisibility(View.VISIBLE);
			holder.price.setText(item.payPrice());//实付
			holder.priceName.setText(MyApp.getInstance().getString(R.string.text_order_title_pay));//实付
			break;
		case Constants.ORDER_STATUS_WAIT_COMMENT:
			
			holder.commentBtn.setVisibility(View.VISIBLE);

			holder.price.setText(item.payPrice());//实付
			holder.priceName.setText(MyApp.getInstance().getString(R.string.text_order_title_pay));//实付

			break;
		case Constants.ORDER_STATUS_ALREADY_CANCEL://已取消状态

			holder.price.setText(item.price());//价格
			holder.priceName.setText(MyApp.getInstance().getString(R.string.text_order_title_goods));//价格

			break;
		case Constants.ORDER_STATUS_ALREADY_COMMENT:

			holder.price.setText(item.payPrice());//实付
			holder.priceName.setText(MyApp.getInstance().getString(R.string.text_order_title_pay));//实付

			break;
		case Constants.ORDER_STATUS_WAIT_SERVICE:

			holder.contactCancelBtn.setVisibility(View.VISIBLE);
			holder.price.setText(item.payPrice());//支付价格
			holder.priceName.setText(MyApp.getInstance().getString(R.string.text_order_title_pay));//实付

			break;

		default:
			break;
		}

		//按钮绑定事件
		holder.payBtn.setOnClickListener(this);
		holder.cancelBtn.setOnClickListener(this);
		holder.contactCancelBtn.setOnClickListener(this);
		holder.commentBtn.setOnClickListener(this);

		holder.payBtn.setTag(position);
		holder.cancelBtn.setTag(position);
		holder.contactCancelBtn.setTag(position);
		holder.commentBtn.setTag(position);

		return convertView;
	}

	protected class ViewHolder {

		@Bind(R.id.id_main_content_service_company_order_name)
		TextView name;

		@Bind(R.id.id_main_content_service_company_order_status)
		TextView status;

		@Bind(R.id.id_main_content_service_company_order_time)
		TextView time;

		@Bind(R.id.id_main_content_service_company_order_site)
		TextView site;

		/** 待付款状态显示价格，其它状态显示支付后实付 */
		@Bind(R.id.id_main_content_service_company_order_price_name)
		TextView priceName;

		/** 待付款状态显示商品价格，其它状态显示支付后价格 */
		@Bind(R.id.id_main_content_service_company_order_price)
		TextView price;

		@Bind(R.id.id_main_content_service_company_order_cancel)
		TextView cancelBtn;

		@Bind(R.id.id_main_content_service_company_order_pay)
		TextView payBtn;

		@Bind(R.id.id_main_content_service_company_order_contact_cancel)
		TextView contactCancelBtn;
		
		@Bind(R.id.id_main_content_service_company_order_comment_btn)
		TextView commentBtn;

		public ViewHolder(View view) {
			ButterKnife.bind(this, view);
		}
	}

	@Override
	public void onClick(View view) {
		
		int position = Integer.parseInt(view.getTag().toString());
		final PurchaseOrder item = list.get(position);

		frag.setCurOrder(item);

		switch (view.getId()) {
		case R.id.id_main_content_service_company_order_pay:
			//			Toast.makeText(frag.getActivity(), "支付订单", Toast.LENGTH_SHORT).show();

			Bundle payParams = new Bundle();
			payParams.putString("order_id", item.getOrderId());
			MyPageHelper.purchaseOrderPay.showMyPage(frag.getActivity(), payParams);

			break;
		case R.id.id_main_content_service_company_order_cancel:

			ViewUtils.confirm(frag.getActivity(), "是否取消订单", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int arg1) {
					//网络请求
					AccessTokenKeeper token = AccessTokenKeeper.readAccessToken(frag.getActivity());

					if (token.isLogin()) {

						final RequestParams cancelParams = new RequestParams();
						cancelParams.put("uid", token.getOpenId());
						cancelParams.put("order_id", item.getOrderId());
						ApiRequest.purchaseOrderCancel.request(frag, cancelParams);

					} else {
						AccountLoginFragment.goWithConfirm(frag.getActivity());
					}
				}
			});

			break;
		case R.id.id_main_content_service_company_order_contact_cancel:

			ViewUtils.confirm(frag.getActivity(), frag.getString(R.string.str_service_contact), new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int arg1) {
					String sereviceTel = OnlineParamUtils.get(Constants.ONLINE_PARAMS_KEY_SERVICE_TEL);
					PhoneUtils.phoneCall(frag.getActivity(), sereviceTel);
				}
			});

			break;
			
		case R.id.id_main_content_service_company_order_comment_btn:

			Bundle args = new Bundle();
			args.putString("order_id", item.getOrderId());
			MyPageHelper.purchaseOrderComment.showMyPage(frag.getActivity(), args);

			break;

		default:
			break;
		}
		
	}
}
